cap program drop tab_uninsured_demographics
program define tab_uninsured_demographics
	use "$indiv_data_path/obs_level_data.dta", clear
	shell mkdir -p "$release/tables/uninsured/"

	keep if choice == 1
	gen best_guess_metal = .
	replace best_guess_metal = 2 if x_av == .6
	replace best_guess_metal = 3 if x_av == .7 | x_av == .73 | x_av == .87
	replace best_guess_metal = 4 if x_av == .8
	rename married_withkids married_wkids
	rename best_guess_incomeoverFPL best_guess_incomeoverfpl
	
	drop withkids married_wkids
	gen withkids = (ndeps > 0)
	gen single_mem = (married==0)&(withkids==0)
	gen unmarried_wkids = (married==0)&withkids
	gen married_nokids = married&(withkids==0)
	gen married_wkids = (married&withkids)
	gen ndeps_positive = ndeps if ndeps > 0
	gen real_urban = inlist(best_guess_ra, 1,2,3) 


	#delimit ;
	collapse 
		(mean) mean_single_mem_ind = single_mem
		(sd)   sd_single_mem_ind   = single_mem
		(p50)  p50_single_mem_ind  = single_mem
		(mean) mean_married_nokids_ind = married_nokids
		(sd)   sd_married_nokids_ind   = married_nokids
		(p50)  p50_married_nokids_ind  = married_nokids
		(mean) mean_unmarried_wkids_ind = unmarried_wkids
		(sd)   sd_unmarried_wkids_ind   = unmarried_wkids
		(p50)  p50_unmarried_wkids_ind  = unmarried_wkids
		(mean) mean_married_wkids_ind = married_wkids
		(sd)   sd_married_wkids_ind   = married_wkids
		(p50)  p50_married_wkids_ind  = married_wkids
		(mean) mean_ndeps_positive = ndeps_positive
		(sd)   sd_ndeps_positive   = ndeps_positive
		(p50)  p50_ndeps_positive  = ndeps_positive
		(mean) mean_sum_concurrent_risk = sum_concurrent_risk
		(sd)   sd_sum_concurrent_risk   = sum_concurrent_risk
		(p50)  p50_sum_concurrent_risk  = sum_concurrent_risk
		(mean) mean_age = age
		(sd)   sd_age   = age
		(p50)  p50_age  = age
		(mean) mean_income = best_guess_incomeoverfpl
		(sd)   sd_income   = best_guess_incomeoverfpl
		(p50)  p50_income  = best_guess_incomeoverfpl
		(mean) mean_over50_ind = over50
		(sd)   sd_over50_ind   = over50
		(p50)  p50_over50_ind  = over50
		(mean) mean_urban_ind = real_urban
		(sd)   sd_urban_ind   = real_urban
		(p50)  p50_urban_ind  = real_urban
		(count) mean_count = subscriberid
		, by(insurance)
	;
	#delimit cr
	
	
	reshape long mean_ sd_ p50_, i(insurance) j(dem_var, string)
	reshape wide mean_ sd_ p50_, i(dem_var) j(insurance)
	local dem_vars = "single_mem_ind married_nokids_ind unmarried_wkids_ind married_wkids_ind ndeps_positive sum_concurrent_risk income age over50_ind urban_ind count"
	word_orderer "`dem_vars'"
	drop_ind_sd "`dem_vars'" "0 1"
	drop p50*
	export delimited using "$release/tables/uninsured/demographics.csv", replace

end
